package com.whu.exp1jsp.dao;


import com.whu.exp1jsp.pojo.Contact;
import com.whu.exp1jsp.pojo.ProductSt;
import com.whu.exp1jsp.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class PurchaseDAO {

    // 数据库连接信息

    // 添加新的购买记录
    public boolean addNewPurchase(long productId, long ownerId, long customerId){
        String sql = "INSERT INTO purchase_st (product_id, owner_id, customer_id, order_time) VALUES (?, ?, ?, CURRENT_TIMESTAMP)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setLong(1, productId);
            ps.setLong(2, ownerId);
            ps.setLong(3, customerId);
            return ps.executeUpdate() > 0;
        }catch (SQLException e){
            e.printStackTrace();
            return false;
        }
    }

    // 获取用户的购买商品列表
    public List<ProductSt> getPurchaseProducts(long userId) throws SQLException {
        List<ProductSt> productList = new ArrayList<>();
        String sql = "SELECT " +
                "p.product_id AS productId, " +
                "p.product_title AS productTitle, " +
                "p.product_intro AS productIntro, " +
                "p.product_price AS productPrice, " +
                "p.product_imgs AS productImgs, " +
                "c.order_time AS publishTime, " +
                "p.owner_id AS ownerId, " +
                "p.location AS location, " +
                "p.status AS status " +
                "FROM product_st p " +
                "JOIN purchase_st c ON p.product_id = c.product_id " +
                "WHERE c.customer_id = ? " +
                "ORDER BY c.order_time DESC";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setLong(1, userId);
            try (ResultSet rs = ps.executeQuery()) {
                while (rs.next()) {
                    ProductSt product = new ProductSt();
                    product.setProductId(rs.getLong("productId"));
                    product.setProductTitle(rs.getString("productTitle"));
                    product.setProductIntro(rs.getString("productIntro"));
                    product.setProductPrice(rs.getDouble("productPrice"));
                    product.setProductImgs(rs.getString("productImgs"));
                    product.setPublishTime(rs.getTimestamp("publishTime").toLocalDateTime());
                    product.setOwnerId(rs.getLong("ownerId"));
                    product.setLocation(rs.getString("location"));
                    product.setStatus(rs.getInt("status"));
                    productList.add(product);
                }
            }
        }
        return productList;
    }

    // 删除购买记录
    public boolean deletePurchase(long productId) throws SQLException {
        String sql = "DELETE FROM purchase_st WHERE product_id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setLong(1, productId);
            return ps.executeUpdate() > 0;
        }
    }

    // 获取联系信息
    public Contact getContact(long productId) throws SQLException {
        String sql = "SELECT o.user_account AS ownerPhone, c.user_account AS customerPhone " +
                "FROM user_st o " +
                "JOIN purchase_st p ON o.user_id = p.owner_id " +
                "JOIN user_st c ON c.user_id = p.customer_id " +
                "WHERE p.product_id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setLong(1, productId);
            try (ResultSet rs = ps.executeQuery()) {
                if (rs.next()) {
                    Contact contact = new Contact();
                    contact.setOwnerPhone(rs.getString("ownerPhone"));
                    contact.setCustomerPhone(rs.getString("customerPhone"));
                    return contact;
                }
            }
        }
        return null;
    }
}
